package com.aifeng.hassan_food.mine

import android.os.Bundle
import android.text.InputFilter
import com.aifeng.base.BaseActivity
import com.aifeng.base.setColor
import com.aifeng.common_ui.base.gone
import com.aifeng.common_ui.base.oneClick
import com.aifeng.common_ui.helper.*
import com.aifeng.hassan_food.R
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.subscribeBy
import com.aifeng.utils.subscribeByNoError
import com.aifeng.viewmodel.WalletViewModel
import kotlinx.android.synthetic.main.activity_wallet_apply.*
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class WalletApplyActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wallet_apply)
        initTitle(title = "申请提现")
    }

    override fun getViewModel() = getViewModel<WalletViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        wallet_input.filters = arrayOf<InputFilter>(DecimalNumberFilter())

        wallet_info.gone()
        wallet_all.setColor(
            "当前账户余额${getString("balance").formatDecimal()}元，全部提现",
            "全部提现"
        )
        when {
            getString("balance").toNotDouble() < 0 -> {
                wallet_hint.text = "提现到微信"
                wallet_day.text = "1天内到账"
            }
            else -> {
                wallet_hint.text = "提现到银行卡"
                val bankNum = getString("bankCard")
                getViewModel().bankName(
                    bankNum,
                    getJsonFromAssets("bank_name.json")
                )
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeBy(
                        onNext = {
                            wallet_type.text = it
                            wallet_day.text = getString("bankCard").bankCardReplaceHeaderWithStar()
                        }
                    )
            }
        }

        wallet_all.oneClick {
            wallet_input.setText(getString("balance").formatDecimal())
            wallet_input.setSelection(wallet_input.text.length)
        }

        bt_apply.oneClick {
            when {
                wallet_input.text.isEmpty() -> {
                    wallet_input.requestFocus()
                    toast("请输入提现金额！")
                    return@oneClick
                }
                wallet_input.text.toTextDouble() > getString("balance").toNotDouble() -> {
                    toast("可提现金额不足！")
                    return@oneClick
                }
            }

            getViewModel().withdraw(
                "userid" to getString("userId"),
                "amount" to wallet_input.textString(),
                "type" to (if (getString("balance").toNotDouble() < 0) "wx" else "bank")
            )
                .bindLifeCycle(getLifecycleOwner())
                .subscribeByNoError(
                    event = getViewModel(),
                    onSuccess = {
                        toast("申请提现提交成功，请耐心等待！")

                        val remain = getString("balance").toNotDouble() - wallet_input.text.toTextDouble()
                        putShared("balance", remain.formatDecimal())

                        ActivityStack.screenManager.popActivities(this::class.java)
                    }
                )
        }
    }

}
